﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassForWinForm
{
    public partial class PageGrid : UserControl
    {
        private int _PageCount = 20;

        public int PageCount
        {
            get { return _PageCount; }
            set { _PageCount = value; }
        }
        private int _selectcount = 0;

        public int Selectcount
        {
            get { return _selectcount; }
            set { _selectcount = value; }
        }
        private int MaxPageCount = 0;
        private DataTable _dt;

        public DataTable dt
        {
            get { return _dt; }
            set { _dt = value; }
        }

        DataGridView _dataGridView1;

        public DataGridView dataGridView1
        {
            get { return _dataGridView1; }
            set { _dataGridView1 = value; }
        }

        private bool _IsCN = true;

        public bool IsCN
        {
            get { return _IsCN; }
            set { _IsCN = value; }
        }

        public PageGrid()
        {
            InitializeComponent();

            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            toolStripButton3.Enabled = false;
            toolStripButton4.Enabled = false;
            toolStripTextBox1.ReadOnly = true;
        }
        public void SetLang()
        {
            if (!_IsCN)
            {
                this.toolStripButton1.Text = "First";
                this.toolStripButton2.Text = "Previous";
                this.toolStripButton3.Text = "Next";
                this.toolStripButton4.Text = "Last";
            }
        }


        public void GridViewPageLoad()
        {
            if (dt != null)
            {
                int pageindex = Selectcount;
                DataTable dt_new = dt.Clone();
                int TotalPage = dt.Rows.Count % PageCount > 0 ? dt.Rows.Count / PageCount + 1 : dt.Rows.Count / PageCount;
                MaxPageCount = TotalPage;
                if (pageindex >= TotalPage)
                {
                    pageindex = TotalPage - 1;
                }
                if (TotalPage == 0)
                {
                    pageindex = 0;
                }
                if (dt != null)
                {

                    int start = pageindex * PageCount;
                    int end = start + PageCount > dt.Rows.Count ? dt.Rows.Count : start + PageCount;
                    for (int i = start; i < end; i++)
                    {
                        dt_new.Rows.Add(dt.Rows[i].ItemArray);
                    }
                }
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = dt_new;

                //toolStripLable1.Text = string.Format("{0}/{1}", (pageindex + 1).ToString(), TotalPage);
                toolStripTextBox1.Text = (pageindex + 1).ToString();
                if (TotalPage == 0)
                {
                    toolStripTextBox1.Text = "0";
                }
                toolStripLabel2.Text = MaxPageCount.ToString();
                //PageGrid_Click(this, null);

                toolStripButton1.Enabled = true;
                toolStripButton2.Enabled = true;
                toolStripButton3.Enabled = true;
                toolStripButton4.Enabled = true;
                toolStripTextBox1.ReadOnly = false;
            }
            else
            {
                toolStripButton1.Enabled = false;
                toolStripButton2.Enabled = false;
                toolStripButton3.Enabled = false;
                toolStripButton4.Enabled = false;
                toolStripTextBox1.ReadOnly = true;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Selectcount = 0;
            GridViewPageLoad();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Selectcount = Selectcount > 0 ? Selectcount - 1 : 0;
            GridViewPageLoad();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Selectcount = Selectcount < MaxPageCount - 1 ? Selectcount + 1 : MaxPageCount - 1;
            GridViewPageLoad();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Selectcount = MaxPageCount - 1;
            GridViewPageLoad();
        }
        private void toolStripComboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            int input = 0;

            if (toolStripTextBox1.Text.Trim().Length > 0)
            {
                if (int.TryParse(toolStripTextBox1.Text.Trim(), out input))
                {
                    if (input < 1 || input > MaxPageCount)
                    {
                        //MessageBox.Show("输入的页数范围不存在");
                        toolStripTextBox1.Text = (Selectcount + 1).ToString();
                    }
                }
                else
                {
                    //MessageBox.Show("输入错误");
                    toolStripTextBox1.Text = (Selectcount + 1).ToString();
                }
            }
            else
            {
                input = 1;
            }

            if (e.KeyCode == Keys.Enter)
            {
                Selectcount = input - 1;
                GridViewPageLoad();
            }

        }

        private void PageGrid_Click(object sender, EventArgs e)
        {
            //GridViewPageLoad();
        }


    }
}
